﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Configuration;
using WowRaidListTracker.DAL;
using WowRaidListTracker.core.Application;
using System.Diagnostics;

namespace WowRaidListTracker.GUI
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private ListTrackerDal dal;
        private MemberManager memberManager;
        private ListManager listManager;

        public MainWindow()
        {
            try
            {
                memberManager = new MemberManager();
                memberManager.MemberUpdated += new MemberManager.MemberUpdateEventHandler(memberManager_MemberUpdated);
                listManager = new ListManager();
                listManager.MemberListUpdated += new ListManager.MemberListUpdateEventHandler(listManager_MemberListUpdated);
                // we need to initialize the actual window components now after the tab manager classes are loaded
                InitializeComponent();
                dal = new ListTrackerDal();
                classSelector.DataContext = CharacterClass.CharacterClassCollection;
                classSelector.SelectedIndex = 0;
                //classSelector.SelectionChanged += new SelectionChangedEventHandler(classSelector_SelectionChanged);
                raidMembersList.DataContext = memberManager.RaidMembersList;
                ccSpecialization1.DataContext = new List<CharacterSpecialization>(CharacterSpecialization.CharacterSpecializationCollection);
                ccSpecialization2.DataContext = new List<CharacterSpecialization>(CharacterSpecialization.CharacterSpecializationCollection);
                ccSpecialization1.SelectedIndex = 0;
                ccSpecialization2.SelectedIndex = 0;
                RaidMemberList.LoadTypes();
                ListSelector.DataContext = new List<RaidMemberList.RaidMemberListType>(RaidMemberList.ListTypes);
                ListSelector.SelectedIndex = 0;
                CurrentSelectedList.DataContext = listManager.RaidMemberList;
            }
            catch (Exception e)
            {
                Trace.WriteLine("Exception caught. {0}", e.Message);
            }
            // attach events
            addMember.Click += new RoutedEventHandler(addMember_Click);
            exitMenuItem.Click += new RoutedEventHandler(exitMenuItem_Click);
            ListSelector.SelectionChanged += new SelectionChangedEventHandler(ListSelector_SelectionChanged);
            ReceiveLootButton.Focusable = false;
            ReceiveLootButton.Click += new RoutedEventHandler(ReceiveLootButton_Click);
        }

        void ReceiveLootButton_Click(object sender, RoutedEventArgs e)
        {
            if (CurrentSelectedList.SelectedIndex > -1)
            {
                RaidMemberList rml = CurrentSelectedList.SelectedItem as RaidMemberList;
                string lootString = lootItem.Text;
                if (lootString.Length < 1)
                    lootString = "Loot item not entered";
                rml.ReceiveLoot(lootString);
                listManager.ShiftRanks(rml);
                CurrentSelectedList.Items.Refresh();
            }
        }

        void ListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ListSelector.SelectedIndex > 0)
            {
                listManager.SetCurrentList((ListSelector.SelectedItem as RaidMemberList.RaidMemberListType).TypeId);
                CurrentSelectedList.DataContext = listManager.RaidMemberList;
                CurrentSelectedList.Items.Refresh();
            }
            e.Handled = true;
        }

        void addMember_Click(object sender, RoutedEventArgs e)
        {
            string message = string.Empty;
            if (classSelector.SelectedIndex == 0)
            {
                message += "Charaacter class not selected\n";
            }
            if (ccSpecialization1.SelectedIndex == 0 || ccSpecialization2.SelectedIndex == 0)
            {
                message += "Character class specializations not selected\n";
            }
            if (newRaidMemberName.Text.Length < 1)
            {
                message += "Raid member name not entered";
            }

            if (message.Length > 0)
            {
                ShowMessageBox(message, "Add Member Error");
            }
            else
            {
                RaidMember rm = new RaidMember()
                {
                    CharacterClassId = (classSelector.SelectedItem as CharacterClass).Id,
                    CharacterClassName = (classSelector.SelectedItem as CharacterClass).Name,
                    Name = newRaidMemberName.Text,
                    IsPresent = false,
                    PrimarySpecialization = (ccSpecialization1.SelectedItem as CharacterSpecialization).Id,
                    SecondarySpecialization = (ccSpecialization2.SelectedItem as CharacterSpecialization).Id,
                    TertiarySpecialization = 5
                };
                memberManager.AddMember(rm);
            }
        }

        void classSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            
        }

        void listManager_MemberListUpdated(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
        }

        void memberManager_MemberUpdated(object sender, EventArgs e)
        {
            RaidMember rm = (sender as RaidMember);
            raidMembersList.Items.Refresh();
            // add the member to the appropriate lists based on specialization
            if (rm.PrimarySpecialization != 5)
            {
                listManager.AddMemberToList(Convert.ToInt32(RaidMemberList.RaidMemberListType.GetTypeBySpecialization(rm.PrimarySpecialization)), rm);
            }
            if (rm.SecondarySpecialization != 5)
            {
                listManager.AddMemberToList(Convert.ToInt32(RaidMemberList.RaidMemberListType.GetTypeBySpecialization(rm.SecondarySpecialization)), rm);
            }
            ResetAddMemberUI();
        }

        void exitMenuItem_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void ShowMessageBox(string messageBoxText, string caption)
        {
            MessageBoxButton button = (MessageBoxButton)Enum.Parse(typeof(MessageBoxButton), "OK");
            MessageBoxImage icon = (MessageBoxImage)Enum.Parse(typeof(MessageBoxImage), "Exclamation");
            MessageBoxResult defaultResult = (MessageBoxResult)Enum.Parse(typeof(MessageBoxResult), "None");
            MessageBoxOptions options = (MessageBoxOptions)Enum.Parse(typeof(MessageBoxOptions), "None");

            MessageBoxResult result = MessageBox.Show(messageBoxText, caption, button, icon, defaultResult, options);
        }

        private void ResetAddMemberUI()
        {
            newRaidMemberName.Text = string.Empty;
            ccSpecialization1.SelectedIndex = 0;
            ccSpecialization2.SelectedIndex = 0;
            classSelector.SelectedIndex = 0;
        }

        private void rdMemberPresent_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox cb = (sender as CheckBox);
            int memberId = Convert.ToInt32(cb.Tag);
            
        }

        private void tabControl1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.OriginalSource.GetType() == typeof(TabControl))
            {
                if (tabControl1.SelectedIndex == 1 && !e.Handled)
                {
                    listManager.LoadLists();
                    ListSelector.SelectedIndex = 0;
                }
            }
        }
    }
}
